﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;
//using Kpmg.Projects.OperatingTool;

public partial class user_SearchTicket : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Search_Click(object sender, EventArgs e) 
    {
        string ticketNum = TicketNum.Text;
        string requestor = Requestor.Text;
        string assignedTo = AssignedTo.Text;
        string startDate = ListStartDate.Text;
        string endDate = ListEndDate.Text;

        string SearchName = SaveName.Text;
        string OwnerStaffNo = SaveOwnerStaffNo.Text;
        string iid = Utilities.getLoginIID();
        Staff staff = StaffDAO.GetObjectByUserId(iid);
        string CreatedUser = staff.StaffNo;

        string conditionName = SaveName.Text;

        IList<SearchCondition> ConList = new List<SearchCondition>();

        if (!string.IsNullOrEmpty(ticketNum))
        {
            SearchCondition c = new SearchCondition(SearchKey.Ticket_No, SearchOperator.Equal, ticketNum, SearchDataType.String);
            ConList.Add(c);
        }

        if (!string.IsNullOrEmpty(requestor))
        {
            SearchCondition c = new SearchCondition(SearchKey.Requestor_Staff_No, SearchOperator.Equal, requestor, SearchDataType.String);
            ConList.Add(c);
            
        }

        if (!string.IsNullOrEmpty(assignedTo))
        {
            SearchCondition c = new SearchCondition(SearchKey.Assigned_Staff_No, SearchOperator.Equal, assignedTo, SearchDataType.String);
            ConList.Add(c);
        }

        if (!string.IsNullOrEmpty(startDate))
        {
            SearchCondition c = new SearchCondition(SearchKey.Created_Time, SearchOperator.GreatOrEqual, startDate, SearchDataType.Date);
            ConList.Add(c);
        }

        if (!string.IsNullOrEmpty(endDate))
        {
            SearchCondition c = new SearchCondition(SearchKey.Created_Time, SearchOperator.LessOrEqual, endDate, SearchDataType.Date);
            ConList.Add(c);
        }

        if (HiddenField1.Value == "b")
        {
            Search search = new Search();
            search.Name = SearchName;
            search.OwnerStaffNo = OwnerStaffNo;
            search.CreatedUser = CreatedUser;
            search.IsPredefined = false;
            search.CreatedTime = DateTime.Now;
            ((List<SearchCondition>)search.Conditions).AddRange(ConList);
            SearchOM.CreateSearch(search, CreatedUser);
        }


        if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate)
            && DateTime.Parse(startDate) > DateTime.Parse(endDate))
        {
            //endDate = "The EndDate must less or equal than StartDate!";
            Remind.Text = "The date is wrong";
        }
        else
        { 
            Session["SearchContext"] = ConList;
            string url = "~/Pages/View.aspx?SearchName=" + SearchName + "&OwnerStaffNo=" + OwnerStaffNo;
            Response.Redirect(url);
        }
    }

    protected void btn_myview_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Pages/MyView.aspx");
    }
}
